const {
  SERVICE_MODE,
  APPROVEAL_STATUS,
  USER_TYPE,
  WORK_ORDER_STATUS,
  WORK_ORDER_CREATE_MODE
} = require('../../constant/index')
const {
  getUserType,
  getUserBaseInformation
} = require('../../utils/strorage')
const {
  updateResponseOnPhone
} = require('../../api/workOrderResponse')
const {
  queryAllEventLevel,
  updateEventLevel
} = require('../../api/eventLevel')
const {
  to
} = getApp()
Component({
  options: {
    addGlobalClass: true
  },
  properties: {
    data: {
      type: Object,
      value: {}
    },
    showApproveStatus: Boolean
  },
  data: {
    SERVICE_MODE,
    WORK_ORDER_STATUS,
    APPROVEAL_STATUS,
    WORK_ORDER_CREATE_MODE,
    USER_TYPE,
    userType: '',
    userInfo: {},
    eventLevel: [],
    pickerValue: '',
    pickerLabel: '',
    originEventLevel: ''
  },
  observers: {
    data: function(val) {
      this.setData({
        userType: getUserType(),
        userInfo: getUserBaseInformation(),
        originEventLevel: this.data.data.eventLevelId || ""
      })
    }
  },
  lifetimes: {
    attached() {
      this.setData({
        userType: getUserType(),
        userInfo: getUserBaseInformation(),
        originEventLevel: this.data.data.eventLevelId || "",
        pickerLabel: this.data.data.eventLevelName || "",
        pickerValue: this.data.data.eventLevelId
      })
      this.getQueryAllEventLevel()
    }
  },
  methods: {
    async getQueryAllEventLevel() {
      const [err, data] = await to(queryAllEventLevel({}))
      if (!err) {
        this.setData({
          eventLevel: data
        })
        return Promise.resolve()
      }
      return Promise.reject(err)
    },

    bindPickerChange(event) {
      const item = this.data.eventLevel[+event.detail.value];
      this.setData({
        pickerLabel: item.eventLevelName,
        ['data.eventLevelName']: item.eventLevelName,
        pickerValue: item.eventLevelId
      })
    },
    previewImage(e) {
      wx.previewImage({
        current: e.currentTarget.id, // 当前显示图片的http链接
        urls: this.data.data.imagePathList, // 需要预览的图片http链接列表
      });
    },

    onCallPhone() {
      const item = this.data.data
      wx.makePhoneCall({
        phoneNumber: item.launchUserMobile,
        success: (res) => {
          this.toCallPhone()
        },
        fail: (res) => {},
        complete: (res => {})
      })
    },

    onCallPhoneByMaintainer() {
      const item = this.data.data
      wx.makePhoneCall({
        phoneNumber: item.maintainerMobile,
        success: (res) => {},
        fail: (res) => {},
        complete: (res => {})
      })
    },

    async toCallPhone() {
      const item = this.data.data
      await to(updateResponseOnPhone({
        workOrderId: item.workOrderId
      }))
    },

    onChangeEventLevel() {
      wx.showModal({
        title: '是否保存?',
        complete: (res) => {
          if (res.confirm) {
            this.updateEventLevel();
          }
        }
      })
    },

    async updateEventLevel() {
      const params = {
        workOrderId: this.data.data.workOrderId,
        operationType: 18,
        eventLevelId: this.data.pickerValue
      }

      const [err] = await to(updateEventLevel(params))

      if (!err) {
        this.triggerEvent('update')
      }
    }
  }
})